package com.steelkiwi.cropiwa.image;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Environment;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author yarolegovich
 * 25.02.2017.
 */
public class CropArea {

    public static CropArea create(RectF coordinateSystem, RectF imageRect, RectF cropRect) {
        return new CropArea(
                moveRectToCoordinateSystem(coordinateSystem, imageRect),
                moveRectToCoordinateSystem(coordinateSystem, cropRect));
    }

    private static Rect moveRectToCoordinateSystem(RectF system, RectF rect) {
        float originX = system.left, originY = system.top;
        return new Rect(
                Math.round(rect.left - originX), Math.round(rect.top - originY),
                Math.round(rect.right - originX), Math.round(rect.bottom - originY));
    }

    private final Rect imageRect;
    private final Rect cropRect;

    public CropArea(Rect imageRect, Rect cropRect) {
        this.imageRect = imageRect;
        this.cropRect = cropRect;
    }

    public Bitmap applyCropTo(Bitmap bitmap) {
        Bitmap immutableCropped = Bitmap.createBitmap(bitmap,
                findRealCoordinate(bitmap.getWidth(), cropRect.left, imageRect.width()),
                findRealCoordinate(bitmap.getHeight(), cropRect.top, imageRect.height()),
                findRealCoordinate(bitmap.getWidth(), cropRect.width(), imageRect.width()),
                findRealCoordinate(bitmap.getHeight(), cropRect.height(), imageRect.height()));
//        return immutableCropped.copy(immutableCropped.getConfig(), true);
//        return copy(immutableCropped);


        return immutableCropped;
    }


    private int findRealCoordinate(int imageRealSize, int cropCoordinate, float cropImageSize) {
        return Math.round((imageRealSize * cropCoordinate) / cropImageSize);
    }


    /**
     * 根据原位图生成一个新的位图，并将原位图所占空间释放
     *
     * @param srcBmp 原位图
     * @return 新位图
     */
    public static Bitmap copy(Bitmap srcBmp) {

        Bitmap destBmp = null;

        try {

            //创建一个临时文件
            File file = new File(Environment.getExternalStorageDirectory().getPath()+"/temp/tmp.txt");
            file.getParentFile().mkdirs();


            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");


            int width = srcBmp.getWidth();
            int height = srcBmp.getHeight();


            FileChannel channel = randomAccessFile.getChannel();
            MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, width * height * 4);
            //将位图信息写进buffer
            srcBmp.copyPixelsToBuffer(map);

            //释放原位图占用的空间
            srcBmp.recycle();

            //创建一个新的位图
            destBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            map.position(0);
            //从临时缓冲中拷贝位图信息
            destBmp.copyPixelsFromBuffer(map);

            channel.close();
            randomAccessFile.close();
        } catch (Exception ex) {

            destBmp = null;
        }

        return destBmp;
    }

}
